﻿/**
 * Label
 * 
 * @version 2012.01.12
 * @author  Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
 */

#region XNA Libraries

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

#endregion

namespace main.sdk.display.object2d
{

    public sealed class Label : Object2D
    {

        #region Static Variables

        public static SpriteFont DefaultSpriteFont = Program.Self.Content.Load<SpriteFont>("RSC/Font/Arial");

        #endregion

        #region Instance Variables

        private SpriteFont _SpriteFont = DefaultSpriteFont;

        public SpriteFont SpriteFont
        {
            get { return _SpriteFont; }
            set
            {
                _SpriteFont      = value == null ? DefaultSpriteFont : value;
                SourceRectangle.X      = 0;
                SourceRectangle.Y      = 0;
                Vector2 size           = _SpriteFont.MeasureString(_Text);
                SourceRectangle.Width  = (int)size.X;
                SourceRectangle.Height = (int)size.Y;
            }
        }

        public string AssetName
        {
            // TODO: get { return ""; }
            set { SpriteFont = Program.Self.Content.Load<SpriteFont>(value); }
        }

        private string _Text = "";

        public string Text
        {
            get { return _Text; }
            set
            {
                _Text = value == null ? "" : value;
                SourceRectangle.X      = 0;
                SourceRectangle.Y      = 0;
                Vector2 size           = _SpriteFont.MeasureString(_Text);
                SourceRectangle.Width  = (int)size.X;
                SourceRectangle.Height = (int)size.Y;
            }
        }

        public bool  Outline      = false;
        public int   OutlineSize  = 1;
        public float OutlineAlpha = 0.5f;
        public Color OutlineColor = Color.Black;

        #endregion

        #region Constructor & Destructor

        public Label(string text = null)
        {
            if (text != null)
                Text = text;
        }

        public Label(SpriteFont spriteFont, string text = null)
        {
            if (spriteFont != null)
                SpriteFont = spriteFont;
            if (text != null)
                Text = text;
        }

        public override void Dispose()
        {
            base.Dispose();
            _SpriteFont = null;
            _Text       = null;
        }

        #endregion

        #region Drawing

        public override void Draw(SpriteBatch spriteBatch)
        {
            // TODO: BETTER OUTLINE ALGORITHM
            if (Outline)
            {
                for (int y = -OutlineSize; y <= OutlineSize; y++)
                {
                    for (int x = -OutlineSize; x <= OutlineSize; x++)
                    {
                        if ((x == 0) && (y == 0))
                            continue;
                        float outlineRealAlpha =  (float)(OutlineAlpha / (Math.Sqrt(x * x + y * y) / OutlineSize));
                        spriteBatch.DrawString(_SpriteFont, _Text, new Vector2(Position.X + x, Position.Y + y), Color.Lerp(Color.Transparent, OutlineColor, _Alpha * outlineRealAlpha), Rotation, Origin, Scale, Effects, LayerDepth);
                    }
                }
            }
            spriteBatch.DrawString(_SpriteFont, _Text, Position, Color.Lerp(Color.Transparent, this.Color, _Alpha), Rotation, Origin, Scale, Effects, LayerDepth);
        }

        #endregion

    }

}